import java.util.Stack;

public class Sort {

    public static void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                } else {
//                    array[j+1]=tmp;
                    break;
                }
            }
            array[j + 1] = tmp;//包含了一个特殊情况:j<0时,第二层for循环进不来
        }
    }

    public static void shellSort(int[] array) {
        int gap = array.length;
        while (gap > 1) {
            gap /= 2;
            shell(array, gap);
        }
    }

    //每组进行插入排序
    private static void shell(int[] array, int gap) {
        //i++,交替进行插入排序
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (array[j] > tmp) {
                    array[j + gap] = array[j];
                } else {
//                    array[j+1]=tmp;
                    break;
                }
            }
            array[j + gap] = tmp;//包含了一个特殊情况:j<0时,第二层for循环进不来
        }
    }

    private static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    //选择排序
    public static void selectSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            swap(array, minIndex, i);
        }
    }

    public static void selectSort2(int[] array) {
        int left = 0;
        int right = array.length - 1;
        while (left < right) {
            int minIndex = left;
            int maxIndex = left;
            for (int i = left + 1; i <= right; i++) {
                if (array[i] < array[minIndex]) {
                    minIndex = i;
                }
                if (array[i] > array[maxIndex]) {
                    maxIndex = i;
                }
            }
            swap(array, minIndex, left);
            //如果最大值是left下标,那么上面交换完成后
            //最大值跑到了最小值的位置,所以要更新最大值的下标
            if (maxIndex == left) {
                maxIndex = minIndex;
            }
            swap(array, maxIndex, right);
            left++;
            right--;
        }
    }

    private static void createBigHeap(int[] array) {
        for (int parent = (array.length - 1 - 1) / 2; parent >= 0; parent--) {
            siftDown(parent, array, array.length);
        }
    }

    private static void siftDown(int parent, int[] array, int end) {
        int child = 2 * parent + 1;
        while (child < end) {
            if (child + 1 < end && array[child] < array[child + 1]) {
                child++;
            }
            //child下标 是左右孩子最大值的下标
            if (array[child] > array[parent]) {
                swap(array, child, parent);
                parent = child;
                child = 2 * parent + 1;
            } else {
                break;
            }
        }
    }

    public static void heapSort(int[] array) {
        createBigHeap(array);

        int end = array.length - 1;
        while (end >= 0) {
            swap(array, 0, end);
            siftDown(0, array, end);
            end--;
        }
    }

    //交换排序-->冒泡排序
    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] > array[j + 1]) {
                    swap(array, j, j + 1);
                    flg = true;
                }
            }
            if (!flg) {
                break;
            }
        }
    }

    public static void quickSort(int[] array) {
        quick(array, 0, array.length - 1);
    }

    private static void quick(int[] array, int start, int end) {
        if (start >= end) {
            return;
        }
        //加入插入排序
        if(end-start+1<= 10){
            insertSortRange(array,start,end);
            return;
        }

        //优化部分,增加了三数取中
        int index=midThreeNum(array,start,end);
        swap(array,index,start);
        //



        int par = partition(array, start, end);
        quick(array, start, par - 1);
        quick(array, par + 1, end);
    }

    private static int partition(int[] array, int left, int right) {
        int i = left;
        int tmp = array[left];
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            while (left < right && array[left] <= tmp) {
                left++;
            }
            swap(array, left, right);
        }
        //left和right 相遇了
        swap(array, left, i);
        return left;
    }

    private static int partition2(int[] array, int left, int right) {
        int tmp=array[left];
        while(left<right){
            while(left<right && array[right]>tmp){
                right--;
            }
            array[left]=array[right];
            while(left<right && array[left]<tmp){
                left++;
            }
            array[right]=array[left];
        }
            array[left]=tmp;
        return left;

    }
    //优化快排
    //返回值是下标
    private static int midThreeNum(int[] array,int left,int right){
        int mid=(left+right)/2;
        if(array[left]>array[right]){
            if(array[mid]<array[left]){
                return left;
            }else if(array[mid]>array[right]){
                return right;
            }else{
                return mid;
            }
        }else{
            if(array[mid]<array[right]){
                return right;
            }else if(array[mid]>array[left]){
                return left;
            }else{
                return mid;
            }
        }
    }
//指定范围的插入排序
    public static void insertSortRange(int[] array,int left,int right) {
        for (int i = left+1; i <=right; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= left; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                } else {
//                    array[j+1]=tmp;
                    break;
                }
            }
            array[j + 1] = tmp;//包含了一个特殊情况:j<0时,第二层for循环进不来
        }
    }
//快排的非递归-- >用栈
    public static void quickSortNor(int[] array) {
        Stack<Integer> stack=new Stack<>();
        int left=0;
        int right=array.length-1;
        int par=partition(array,left,right);

        if(par> left+1){
            stack.push(left);
            stack.push(par-1);
        }
        if(par< right-1){
            stack.push(par+1);
            stack.push(right);
        }

        while(!stack.isEmpty()){
            right=stack.pop();
            left=stack.pop();
            par=partition(array,left,right);
            if(par> left+1){
                stack.push(left);
                stack.push(par-1);
            }
            if(par< right-1){
                stack.push(par+1);
                stack.push(right);
            }
        }
    }

    //归并排序
    public static void mergeSort(int[] array){
        mergeSortFun(array,0, array.length-1);
    }

    private static void mergeSortFun(int[] array,int left,int right){
        if (left >= right){
            return;
        }

        int mid=(left+right)/2;
        mergeSortFun(array,left,mid);
        mergeSortFun(array,mid+1,right);

        //合并
        merge(array,left,right,mid);
    }

    private static void merge(int[] array,int left,int right,int mid){
        int k=0;
        int[] tmp=new int[right-left+1];
        int s1=left;
        int e1=mid;
        int s2=mid+1;
        int e2=right;
        while(s1<=e1 && s2<=e2){
            if(array[s1] < array[s2]){
                tmp[k++]=array[s1++];
            }else{
                tmp[k++]=array[s2++];
            }
        }
        //一个走完了,另一个放入tmp数组中
        while(s1 <= e1){
            tmp[k++]=array[s1++];
        }
        while(s2 <= e1){
            tmp[k++]=array[s2++];
        }
        //tmp数组中所有元素都有序了,
        //将tmp拷贝到array数组中
        for (int i = 0; i < k; i++) {
            array[i+left]=tmp[i];
        }
    }

    //非递归 实现 归并排序
    public static void mergeSortNor(int[] array){
        int gap=1;
        while(gap < array.length){
            for (int i = 0; i < array.length; i=i+ 2*gap) {
                int left=i;
                int mid=left+gap-1;
                //mid可能越界
                if(mid>= array.length){
                    mid=array.length-1;
                }
                int right=mid+gap;
                //right可能越界
                if(right>=array.length){
                    right=array.length-1;
                }
                //合并
                merge(array,left,right,mid);
            }
            gap*=2;
        }
    }

    //计数排序
    public static void countSort(int[] array){
        //1.找到数组中 最大值 和 最小值
        int minVal=array[0];
        int maxVal=array[0];
        for (int i = 0; i < array.length; i++) {
            if(maxVal<array[i]){
                maxVal=array[i];
            }
            if(minVal>array[i]){
                minVal=array[i];
            }
        }
        //1.定义count数组
        int[] count=new int[maxVal-minVal+1];
        int k=0;//计数数组的大小

        //3.遍历array数组 把值放入 计数数组中
        for (int i = 0; i < array.length; i++) {
            int val=array[i];
            count[val-minVal]++;
        }

        //4.计数数组 存好了 数字出现的 次数 ,接下来 开始遍历计数数组
        int index=0;
        for (int i = 0; i < count.length; i++) {
            while(count[i]>0){
                array[index]=i+minVal;
                index++;
                count[i]--;
            }
        }

    }
}